package hczc.longcai.com.hccrowdfunding.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import hczc.longcai.com.hccrowdfunding.beans.UserInfo;

/**
 * 用户信息数据库操作类
 * Created by Administrator on 2016/2/26.
 */
public class DBUserInfoController {

    private static final String TAG = "DBUserInfoController";
    private final SQLiteDatabase db;
    private Context context;
    private ContentValues contentValues;

    public DBUserInfoController(Context context) {
        this.context = context;
        DBHelper dbHelper = new DBHelper(this.context);
        db = dbHelper.getReadableDatabase();

    }

    /**
     * 清空用户信息表
     */
    public void clearBanners() {
        String clearBanner = "DELETE FROM " + DBHelper.Minfo.TABLE_USERS;
        String setZero = "UPDATE sqlite_sequence SET seq = 0 WHERE name = '" + DBHelper.Minfo.TABLE_USERS+ "'";
        db.execSQL(clearBanner);
        db.execSQL(setZero);
    }

    /**
     * 查询用户信息
     * @return 存储用户所有信息
     */

    public UserInfo findUsers(){
        UserInfo userInfo = null;
        Cursor cursor = db.query(DBHelper.Minfo.TABLE_USERS, null, null, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()){
                String id = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_ID));
                String name = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_NAME));
                String email = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_EMAIL));
                String phone = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_PHONE));
                String sex = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_SEX));
                String realName = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_REAL_NAME));
                String idcard = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_IDCARD));
                String freeze = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_MONEY_FREEZE));
                String collect = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_MONEY_COLLECT));
                String money = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_NAME_USER_ACCOUNT_MONEY));
                String head = cursor.getString(cursor.getColumnIndex(DBHelper.Minfo.COLUMN_MANE_USER_HEAD));
                userInfo = new UserInfo(id,name,email,phone,sex,realName,idcard,freeze,collect,money,head);
            }
        }

        return userInfo;
    }

    /**
     * 将读取到的用户信息存入数据库
     * @param userInfo 读取到的用户信息
     */
    public void saveInfo(UserInfo userInfo) {
        clearBanners();
        UserInfo info = null;
        if (userInfo != null) {
            info = userInfo;
            if (contentValues == null) {
                contentValues = new ContentValues();
            }
            contentValues.putNull(DBHelper.Minfo._ID);
            String id = info.getId();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_ID, id);
            String name = info.getName();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_NAME, name);
            String email = info.getEmail();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_EMAIL, email);
            String phone = info.getPhone();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_PHONE, phone);
            String sex = info.getSex();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_SEX,sex);
            String realName = info.getRealName();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_REAL_NAME,realName);
            String idCard = info.getIDCard();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_IDCARD,idCard);
            String freeze = info.getFreeze();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_MONEY_FREEZE,freeze);
            String collect = info.getCollect();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_MONEY_COLLECT,collect);
            String accountMoney = info.getAccountMoney();
            contentValues.put(DBHelper.Minfo.COLUMN_NAME_USER_ACCOUNT_MONEY,accountMoney);
            String head = info.getHead();
            contentValues.put(DBHelper.Minfo.COLUMN_MANE_USER_HEAD,head);

            long insert = db.insert(DBHelper.Minfo.TABLE_USERS, null, contentValues);
            Log.d(TAG,"插入成功！");
        }

        contentValues.clear();
    }

    /**
     * 关闭当前数据库连接
     */
    public void closeDB() {
        db.close();
    }
}
